/**
 * 
 */
package com.teach.servlet.admin;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.teach.entity.User;
import com.teach.service.UserUpdateService;
import com.teach.service.impl.UserUpdateServiceImpl;

/**
 * @author Liulianqiu
 *2020年8月1日
 */
public class UserUpdateServlet extends HttpServlet{

	UserUpdateService userUpdateService = new UserUpdateServiceImpl();
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");//设置post请求正文编码格式
		User user = new User();
		user.setLlq_name(request.getParameter("userName"));
		user.setLlq_age(Integer.valueOf(request.getParameter("age")));
		user.setLlq_birthday(Date.valueOf(request.getParameter("userBrithdate")));
		try {
			if(Boolean.valueOf(request.getParameter("userSex"))) {
				user.setLlq_sex("男");
			}else {
				user.setLlq_sex("女");
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			throw new RuntimeException("数据格式错误");
		}
	//调用业务逻辑层接口
		try {
			userUpdateService.update(user, Integer.valueOf(request.getParameter("userId")));
			request.setAttribute("msg", "修改用户成功");
			request.setAttribute("href","admin/display-user.let");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error", "修改用户失败:"+e.getMessage());
		}
		request.getRequestDispatcher("/admin/msg.jsp").forward(request, response);
	}
}
